package com.woniuxy.utils;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

/**
 * @Author: ideaAdmin
 * TODO: 拦截器1 配置文件里面配置的先执行
 * Date: 2022/2/11:10:59
 * Description:
 */

public class MyInterceptor01  implements HandlerInterceptor {
    public static LocalDateTime start ;
    public static DateTimeFormatter  formatter =  DateTimeFormatter.ofPattern("yyyy-SS-ss HH:mm:ss a");


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("开始时间" + formatter.format(start = LocalDateTime.now()));
        System.out.println("MyInterceptor01.preHandle" + "在controller之前执行");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor01.postHandle 在controller执行完后执行");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor01.afterCompletion 在返回jsp页面之后执行" );

        //返回两个时间的差值 为 毫秒
        System.out.println("结束时间" + Duration.between(start,LocalDateTime.now()).toMillis());
    }
}
